package cn.hh.wechatkit.api.card;

import cn.hh.wechatkit.data.returndata.Meta_RMsg_Base;
import cn.hh.wechatkit.data.postdata.card.Meta_CardSeatPostData;
import cn.hh.wechatkit.exception.Wx_Exception_BadNetwork;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidJsonParse;
import cn.hh.wechatkit.exception.Wx_Exception_InvalidResponse;
import cn.hhchat.kit.http.httpUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import okhttp3.Response;

import java.io.IOException;

import static cn.hh.wechatkit.helper.WxConst.UpdateCardSeat_Post_URL;


/**
 * Created this one by HMH on 2017/5/7.
 */
@Slf4j
public class WxAPI_Card_UpdateSeat_Api {

    public static Meta_RMsg_Base updateSeat(Meta_CardSeatPostData seat, String ACCESS_TOKEN) {
        String url = UpdateCardSeat_Post_URL.replace("ACCESS_TOKEN", ACCESS_TOKEN);

        Response response = null;
        try {
            response = httpUtil.postJson(url, seat);
            if (response != null) {
                String body = null;
                try {
                    body = response.body().string();
                    return JSON.parseObject(body, Meta_RMsg_Base.class);
                } catch (IOException e) {
                    log.error(" ==> 解析json发生异常", e);
                    throw new Wx_Exception_InvalidJsonParse("解析json发生异常", e);
                }
            }
        } catch (IOException e) {
            log.error(" ==> 请求失败", e);
            throw new Wx_Exception_BadNetwork("请求失败", e);
        }
        //获取数据
        throw new Wx_Exception_InvalidResponse("没有收到响应，或者请求出错");
    }
}
